package com.asia5b.a5mall.order.adapter

import androidx.core.content.ContextCompat
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.widget.TextView
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.model.shop.AbsShopModel
import com.asia5b.a5mall.middleware.utils.StringUtils
import com.asia5b.a5mall.middleware.utils.push
import com.asia5b.a5mall.order.R
import com.asia5b.a5mall.order.databinding.ItemOrderplaceShopadapterBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * Created by zhangyuncai on 2020/5/9.
 */
class OrderPlaceShopAdapter(var list: ArrayList<AbsShopModel>) :
        BaseQuickAdapter<AbsShopModel, BaseDataBindingHolder<ItemOrderplaceShopadapterBinding>>(R.layout.item_orderplace_shopadapter, list) {


    override fun convert(helper: BaseDataBindingHolder<ItemOrderplaceShopadapterBinding>, item: AbsShopModel) {
        val itemOrderplaceShopadapterBinding = helper.dataBinding!!
        itemOrderplaceShopadapterBinding.data = item

        val goodsRecyclerView = helper.getView<RecyclerView>(R.id.rv_orderplace_shop_goods)
        var absShopGoodsNum=0
        if (item.absShopGoodsList != null) {
            absShopGoodsNum=item.absShopGoodsList!!.size
        }
        val goodsAdapter = OrderListImgAdapter(item.absShopGoodsList?: arrayListOf())
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            item.absShopGoodsList?.run {
                startPlaceGoodsActivity(this)
            }

        }
        helper.itemView.setOnClickListener {
            item.absShopGoodsList?.run {
                startPlaceGoodsActivity(this)
            }
        }
        goodsRecyclerView.layoutManager = GridLayoutManager(context, 4)!!
        goodsRecyclerView.adapter = goodsAdapter

        //商品数量
        val tv_orderplace_shop_goodscn = helper.getView<TextView>(R.id.tv_orderplace_shop_goodscn)
        val goodsCnText = String.format(ResString.get(R.string.共_数_件商品), "${absShopGoodsNum}")
        val spannableString = StringUtils.setTextColor(goodsCnText, "${absShopGoodsNum}", ContextCompat.getColor(context, R.color.common_text_blue))
        tv_orderplace_shop_goodscn.setText(spannableString)

    }

    fun startPlaceGoodsActivity(list:ArrayList<AbsGoodsModel>) {
        if (context is AppCompatActivity) {
            (context as AppCompatActivity).push(ActCons.PlaceGoodListActivity, {
                it.withSerializable("datas", list)
            })
        }
    }

}